﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Appsters
{
    public partial class notes : Form
    {
        private OleDbConnection dbConn;
        private DateTime dt;
        private DateTime dt2;

        public notes()
        {
            InitializeComponent();

            string docPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string notesFolder = System.IO.Path.Combine(docPath, "Jotter\\Saved Files\\Notes");
            System.IO.Directory.CreateDirectory(notesFolder);

            OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder();

            csb["provider"] = "Microsoft.ACE.OLEDB.12.0";
            csb["data source"] = Application.StartupPath + @"\Jotter.accdb";

            string connStr = csb.ConnectionString;

            dbConn = new OleDbConnection(connStr);
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            GraphicsPath g = new GraphicsPath();
            g.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
            Region r = new System.Drawing.Region(g);

            del.Enabled = false;
        }

        private void exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void recent_Click(object sender, EventArgs e)
        {
            string title = "";
            string desc = "";
            string dateDD;
            string dateMM;
            string dateYYYY;
            string dateF;
            
            recent.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
            
            try
            {
                dateDD = this.date.Value.Day.ToString();
                dateMM = this.date.Value.Month.ToString();
                dateYYYY = this.date.Value.Year.ToString();
                dateF = dateDD + "/" + dateMM + "/" + dateYYYY;
                this.dt = DateTime.ParseExact(dateF, "d/M/yyyy", null);
            } catch(Exception r)
            {

            }

            dbConn.Open();

            string qryStr = @"SELECT title, description, dateField FROM Notes";
            OleDbCommand cmd = new OleDbCommand(qryStr, dbConn);
            OleDbDataReader reader = cmd.ExecuteReader();

            try
            {
                while(reader.Read())
                {
                    if (this.dt == (DateTime)reader.GetDateTime(2))
                   {
                       title = (string)reader.GetString(0);
                       desc = (string)reader.GetString(1);
                   }
                    else if (DateTime.Today == (DateTime)reader.GetDateTime(2))
                    {
                        title = (string)reader.GetString(0);
                        desc = (string)reader.GetString(1);
                    }
                }
            } catch(Exception q)
            {

            }
            this.title.Text = title;
            this.writeBox.Text = desc;
            

            dbConn.Close();
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            backButton.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
            profView pv = new profView();
            pv.Show();
            this.Hide();
        }

        private void save_Click(object sender, EventArgs e)
        {
            save.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);

            if((title.Text.Length > 0) & (writeBox.Text.Length > 0))
            {
                dbConn.Open();

                string cmdStr = @"INSERT INTO Notes VALUES (?, ?, ?)";

                OleDbCommand cmd = new OleDbCommand(cmdStr, dbConn);

                cmd.Parameters.AddWithValue("title", title.Text);
                cmd.Parameters.AddWithValue("description", writeBox.Text);
                cmd.Parameters.AddWithValue("dateField", date.Value.ToString("MM-dd-yyyy"));
                cmd.ExecuteNonQuery();

                dbConn.Close();

                MessageBox.Show("Your note has been saved. Click the View Recent Notes button to view it.",
                     "Saved Note");
            }
            else
            {
                MessageBox.Show("Please make sure you included a title and the note when you save.",
                    "Empty note");
            }
            
            title.Clear();
            title.Text = "Title";
            writeBox.Clear();
        }

        private void del_Click(object sender, EventArgs e)
        {
            del.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);

            DialogResult result = MessageBox.Show("Delete all text?", "Clear note",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2);

            if (result == DialogResult.Yes)
            {
                title.Clear();
                title.Text = "Title";
                writeBox.Clear();
            }

            del.Enabled = false;

        }

        private void title_TextChanged(object sender, EventArgs e)
        {
            if (title.Text.Length == 0)
            {
                del.Enabled = false;
            }
            else
            {
                del.Enabled = true;
            }
        }

        private void writeBox_TextChanged(object sender, EventArgs e)
        {
            if (writeBox.Text.Length == 0)
            {
                del.Enabled = false;
            }
            else
            {
                del.Enabled = true;
            }
        }
    }
}
